﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;
using Lamex.Common;
using Lamex.Library.CommonClass;
using DevExpress.Utils;

namespace Lamex.mPPC.UI
{
    public partial class frmPPC_Remain_Board_To_Panel : frmBaseChild
    {
        string strD1, strD2, strCondition;
        string strSql;

        public frmPPC_Remain_Board_To_Panel()
        {
            InitializeComponent();
        }

        private void frmPPC_Remain_Board_To_Panel_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            DateTime now = DateTime.Now;
            dteStart.DateTime = now;
            //dteStart.DateTime = now.AddDays(-(int)now.DayOfWeek + 1 - 7);
            //dteEnd.DateTime = now.AddDays(7 - (int)now.DayOfWeek - 7);

            gridView1.OptionsBehavior.Editable = false;//只读
            gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;//列标题居中
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            gridView1.Columns.Clear();

            getQueryDate();//获取起止时间段

            strSql = @"SELECT OPTIMIZE_DATE AS 优化日期,OPTIMIZE_NAME AS 优化单号,LENGTH AS 长度,WIDTH AS 宽度,SUM(QUANTITY) AS 余料数量
                        FROM View_PPC_RT_GetRemainMaterial
                        WHERE BOARD_THICK=" + comboBoxEditBoard_Thick.Text + @" 
                        AND OPTIMIZE_DATE>='" + strD1 + @"' AND  OPTIMIZE_DATE<='" + strD2 + @"'
                        AND ((LENGTH>=" + textEditMin_Size1.Text + @" AND WIDTH>=" + textEditMin_Size2.Text + @") 
                        OR (LENGTH>=" + textEditMin_Size2.Text + @" AND WIDTH>=" + textEditMin_Size1.Text + @"))
                        GROUP BY OPTIMIZE_DATE,OPTIMIZE_NAME,LENGTH,WIDTH,QUANTITY
                        HAVING QUANTITY>=" + textEditMin_QTY.Text;

            DataTable dt1 = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            
            if (dt1.Rows.Count > 0)
            {
                gridControl1.DataSource = dt1;
            }

            //DevExpress XtraGrid GridColumn 列宽自适应
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;

            if (gridView1.RowCount > 0)
            {
                for (int i = 1; i < gridView1.Columns.Count; i++)
                {
                    gridView1.Columns[i].BestFit();
                }

                gridView1.Columns[0].Width = 80;

                TotalSum();//统计求和

                this.Text = "可开屏风板余料明细 ( 日期:" + strD1 + " - " + strD2 +
                            ", 尺寸:" + textEditMin_Size1.Text + " - " + textEditMin_Size2.Text +
                            ", 余料数量≥" + textEditMin_QTY.Text + " )";

            }
            else
            {
                this.Text = "可开屏风板余料";
                XtraMessageBox.Show("没有找到符合条件的数据！");
            }
        }

        //获取起止时间段
        private void getQueryDate()
        {
            strD1 = dteStart.Text.ToString();

            if (dteEnd.Text == "")
            {
                strD2 = strD1;
            }
            else
            {
                strD2 = dteEnd.Text.ToString();
            }
        }

        private void rbtnCurWeek_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnCurWeek.Checked == true)
            {
                doClearViewAndChartData();

                DateTime now = DateTime.Now;
                dteStart.DateTime = now.AddDays(-(int)now.DayOfWeek + 1);
                dteEnd.DateTime = now.AddDays(7 - (int)now.DayOfWeek);
            }
        }

        private void rbtnLastWeek_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLastWeek.Checked == true)
            {
                doClearViewAndChartData();

                DateTime now = DateTime.Now;
                dteStart.DateTime = now.AddDays(-(int)now.DayOfWeek + 1 - 7);
                dteEnd.DateTime = now.AddDays(7 - (int)now.DayOfWeek - 7);
            }
        }

        private void dteStart_EditValueChanged(object sender, EventArgs e)
        {
            doClearViewAndChartData();
        }

        private void dteEnd_EditValueChanged(object sender, EventArgs e)
        {
            doClearViewAndChartData();
        }

        private void dteStart_Click(object sender, EventArgs e)
        {
            rbtnCurWeek.Checked = false;
            rbtnLastWeek.Checked = false;
        }

        private void dteEnd_Click(object sender, EventArgs e)
        {
            rbtnCurWeek.Checked = false;
            rbtnLastWeek.Checked = false;
        }

        //重置图表
        private void doClearViewAndChartData()
        {
            gridView1.Columns.Clear();
            this.Text = "可开屏风板余料";
        }

        private void TotalSum()
        {
            gridView1.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gridView1.Columns[0].SummaryItem.DisplayFormat = "计数或汇总:";

            if (gridView1.Columns.Contains(gridView1.Columns["优化单号"]))
            {
                gridView1.Columns["优化单号"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
                //gridView1.Columns["优化单号"].SummaryItem.DisplayFormat = "{0:N}";
                gridView1.Columns["优化单号"].SummaryItem.DisplayFormat = "{0:n0}";
                //gridView1.Columns["入仓件数"].Width = 80;
            }

            if (gridView1.Columns.Contains(gridView1.Columns["余料数量"]))
            {
                gridView1.Columns["余料数量"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                //gridView1.Columns["余料数量"].SummaryItem.DisplayFormat = "{0:N}";
                gridView1.Columns["余料数量"].SummaryItem.DisplayFormat = "{0:n0}";
                //gridView1.Columns["工时"].Width = 80;
            }
        }

        #region 实例化标准功能

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 0;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 60;
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Lamex.Common.ExecelHelper.GridViewToExcel(gridView1);
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Lamex.Common.ExecelHelper.GridViewToPrint(gridControl1, this.Text);
        }

        public override void DoView(IButtonInfo sender)
        {
            sbtnView_Click(null, null);
            base.DoView(sender);
        }

        public override void DoExport(IButtonInfo sender)
        {
            导出ToolStripMenuItem_Click(null, null);
            base.DoExport(sender);
        }

        public override void DoPrint(IButtonInfo sender)
        {
            打印ToolStripMenuItem_Click(null, null);
            base.DoPrint(sender);
        }
        
        #endregion

        //基础配置窗体跳转
        private void labelControlGoto_Click(object sender, EventArgs e)
        {
            Loginer.CurrentUser.FunctionId = "menuPPC_Remain_Board_Material_List";
            MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmBaseChildWinForm("View_PPC_RT_GetRemainMaterial", 0, false, false), sender as ToolStripMenuItem);
        }

    }
}